package com.seckill.common.other;

import java.io.Serializable;

/**
 * HTTP响应对象
 *
 * @param <T>
 */
public class ServerResponse<T> implements Serializable {
    // 状态码
    private String status;
    // 消息
    private String message;
    // 数据
    private T data;

    private ServerResponse(String status) {
        this.status = status;
    }

    private ServerResponse(String status, String message) {
        this.status = status;
        this.message = message;
    }

    private ServerResponse(String status, T data) {
        this.status = status;
        this.data = data;
    }

    private ServerResponse(String status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    // HTTP响应方法
    public static <T> ServerResponse<T> createBySuccess() {
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> createBySuccess(String message) {
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode(), message);
    }

    public static <T> ServerResponse<T> createBySuccess(T data) {
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> ServerResponse<T> createBySuccess(String message, T data) {
        return new ServerResponse<>(ResponseCode.SUCCESS.getCode(), message, data);
    }

    public static <T> ServerResponse<T> createByError() {
        return new ServerResponse<>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
    }

    public static <T> ServerResponse<T> createByError(String errorMessage) {
        return new ServerResponse<>(ResponseCode.ERROR.getCode(),errorMessage);
    }

    public static <T> ServerResponse<T> createByError(String errorCode, String errorMessage) {
        return new ServerResponse<>(errorCode,errorMessage);
    }

    public String getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }
}
